﻿#include "stdafx.h"
#include "CreateFileApp.h"
#include "QCreateFileThread.h"

#ifdef WIN32
#include <windows.h>
#else
#include "unistd.h"
#include "sys/sysinfo.h"
#endif

#if defined(_MSC_VER) && (_MSC_VER >= 1600)  
# pragma execution_character_set("utf-8")  
#endif 

CreateFileApp::CreateFileApp(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	connect(ui.pushButton_Browser, SIGNAL(clicked()), this, SLOT(sltBrowser()));
	connect(ui.pushButton_Create, SIGNAL(clicked()), this, SLOT(sltCreate()));

	connect(ui.pushButton_calcSize, SIGNAL(clicked()), this, SLOT(sltCalcSize()));

	
	ui.lineEdit_SubDirNumTop->setValidator(new QIntValidator(1,9999999999,this));
	ui.lineEdit_SubDirNumOther->setValidator(new QIntValidator(1,9999999999,this));

	ui.lineEdit_SubFileNumLast->setValidator(new QIntValidator(1,9999999999,this));
	ui.lineEdit_SubFileNumMid->setValidator(new QIntValidator(1,9999999999,this));

	ui.lineEdit_DirLayerNum->setValidator(new QIntValidator(0,9999999999,this));
	ui.lineEdit_FileKB->setValidator(new QIntValidator(1,9999999999,this));
	ui.lineEdit_ThreadNum->setValidator(new QIntValidator(1,10,this));

	int nThreadNum = 0;
#ifdef WIN32
	SYSTEM_INFO sysInfo;
	GetSystemInfo( &sysInfo );
	nThreadNum = sysInfo.dwNumberOfProcessors;
#else
    nThreadNum = sysconf(_SC_NPROCESSORS_ONLN);
#endif
	ui.lineEdit_ThreadNum->setText(QString::number(nThreadNum));
}

CreateFileApp::~CreateFileApp()
{

}


#define ONE_KB	1024
#define ONE_MB	(1024*1024)
#define ONE_GB	(1024*1024*1024)
/*#define ONE_TB	(1024*1024*1024*1024)*/

static QString FormatSize(quint64 u64Value, int nBit = 2)
{
	if (u64Value == 0)
	{
		return "0 KB";
	}
	QString strRet;
	float fValue;
	// 	if (u64Value >= (quint64)ONE_TB)
	// 	{
	// 		fValue = (float)((double)u64Value / (double)ONE_TB);
	// 		strRet = QString::number(fValue, 'f', 2) + "TB";
	// 	}
	if (u64Value >= ONE_GB)
	{
		fValue = (float)((double)u64Value / (double)ONE_GB);
		strRet = QString::number(fValue, 'f', nBit) + " GB";
	}
	else if (u64Value >= ONE_MB)
	{
		fValue = (float)((double)u64Value / (double)ONE_MB);
		strRet = QString::number(fValue, 'f', nBit) + " MB";
	}
	else if (u64Value >= ONE_KB)
	{
		fValue = (float)((double)u64Value / (double)ONE_KB);
		strRet = QString::number(fValue, 'f', nBit) + " KB";
	}
	else
	{
		strRet = QString::number(u64Value) + " Bytes";
	}
	return strRet;
}

void CreateFileApp::sltCalcSize()
{
	int nSubDirNumTop = ui.lineEdit_SubDirNumTop->text().toInt();
	int nSubDirNumOther = ui.lineEdit_SubDirNumOther->text().toInt();
	int nSubFileNumLast = ui.lineEdit_SubFileNumLast->text().toInt();
	int nSubFileNumMid = ui.lineEdit_SubFileNumMid->text().toInt();
	int nDirLayerNum = ui.lineEdit_DirLayerNum->text().toInt();
	int nFileKB = ui.lineEdit_FileKB->text().toInt();
	QString strSaveDir = ui.lineEdit_SaveDir->text();
	int nThreadNum = ui.lineEdit_ThreadNum->text().toInt();
	int nFileNameLen = ui.lineEdit_FileNameLen->text().toInt();
	bool bCNName = ui.checkBox_CNName->isChecked();

	int nMidDirNum = 0;
	int nLastDirNum = 0;

	for (int i=0; i<nDirLayerNum; i++)
	{
		if ( i == 0)
		{
			nMidDirNum += 1;
		}
		else if (i == 1)
		{
			nMidDirNum += nSubDirNumTop;
		}
		else if (i != nDirLayerNum - 1)
		{
			nMidDirNum += (nSubDirNumTop * (qPow(nSubDirNumOther, i - 1)));
		}
		else
		{
			nLastDirNum += (nSubDirNumTop * (qPow(nSubDirNumOther, i - 1)));
		}
	}
	int nTotalFileNum = nMidDirNum * nSubFileNumMid + nLastDirNum * nSubFileNumLast;
	quint64 size = nTotalFileNum;
	size *= nFileKB;
	size *= 1024;
	QMessageBox::information(this, "total size", FormatSize(size));
}

void CreateFileApp::sltBrowser()
{
	QString directory = QFileDialog::getExistingDirectory(this, "请选择保存文件夹", QDir::currentPath());
	if (!directory.isEmpty())
	{
		ui.lineEdit_SaveDir->setText(directory);
	}
}

void CreateFileApp::sltUpdateStatus(QString strFilePath, int nPos, int nIndex)
{
// 	static int pos_bak = -1;
// 	if (pos_bak != nPos)
	{
		m_vProgressBar[nIndex]->setValue(nPos);
/*		pos_bak = nPos;*/
	}
// 	ui.listWidget->addItem(strFilePath);
// 	ui.listWidget->scrollToBottom();
}

void CreateFileApp::sltCreate()
{
/*	ui.listWidget->clear();*/

	int nSubDirNumTop = ui.lineEdit_SubDirNumTop->text().toInt();
	int nSubDirNumOther = ui.lineEdit_SubDirNumOther->text().toInt();

	int nSubFileNumLast = ui.lineEdit_SubFileNumLast->text().toInt();
	int nSubFileNumMid = ui.lineEdit_SubFileNumMid->text().toInt();

	int nDirLayerNum = ui.lineEdit_DirLayerNum->text().toInt();
	int nFileKB = ui.lineEdit_FileKB->text().toInt();
	QString strSaveDir = ui.lineEdit_SaveDir->text();
	int nThreadNum = ui.lineEdit_ThreadNum->text().toInt();
	int nFileNameLen = ui.lineEdit_FileNameLen->text().toInt();
	bool bCNName = ui.checkBox_CNName->isChecked();


	int nMidDirNum = 0;
	int nLastDirNum = 0;

	for (int i=0; i<nDirLayerNum; i++)
	{
		if ( i == 0)
		{
			nMidDirNum += 1;
		}
		else if (i == 1)
		{
			nMidDirNum += nSubDirNumTop;
		}
		else if (i != nDirLayerNum - 1)
		{
			nMidDirNum += (nSubDirNumTop * (qPow(nSubDirNumOther, i - 1)));
		}
		else
		{
			nLastDirNum += (nSubDirNumTop * (qPow(nSubDirNumOther, i - 1)));
		}
	}
	int nTotalFileNum = nMidDirNum * nSubFileNumMid + nLastDirNum * nSubFileNumLast;
	quint64 size = nTotalFileNum;
	size *= nFileKB;
	size *= 1024;

	QString strMsg = QString("共计：文件个数： %1 文件总大小：%2\n确定开始创建文件？").arg(nTotalFileNum).arg(FormatSize(size));
	if (QMessageBox::question(this, "question", strMsg) != QMessageBox::Ok)
	{
		return;
	}

	for (int i=0; i<m_vProgressBar.size(); i++)
	{
		ui.verticalLayout_Progress->removeWidget(m_vProgressBar[i]);
		delete m_vProgressBar[i];
	}
	m_vProgressBar.clear();

	int nFileNumPreThread = 0;

	nFileNumPreThread = nSubDirNumTop / nThreadNum;	
	for (int i=0; i<nThreadNum; i++)
	{
		QProgressBar* bar = new QProgressBar(this);
		ui.verticalLayout_Progress->addWidget(bar);
		m_vProgressBar.push_back(bar);

		QCreateFileThread* thread = new QCreateFileThread(this);
		thread->m_nStartFileNum = nFileNumPreThread * i;
		if (i == nThreadNum - 1)
		{
			thread->m_nEndFileNum = nSubDirNumTop;
		}
		else
		{
			thread->m_nEndFileNum = thread->m_nStartFileNum + nFileNumPreThread;
		}
		thread->m_nSubDirNumTop = nSubDirNumTop;
		thread->m_nSubDirNumOther = nSubDirNumOther;

		thread->m_nSubFileNumLast = nSubFileNumLast;
		thread->m_nSubFileNumMid = nSubFileNumMid;

		thread->m_nDirLayerNum = nDirLayerNum;
		thread->m_nFileKB = nFileKB;
		thread->m_strSaveDir = strSaveDir;
		thread->m_nIndex = i;
		thread->m_bCNName = bCNName;
		thread->m_nFileNameLen = nFileNameLen;
		connect(thread, SIGNAL(sigUpdateStatus(QString, int, int)), this, SLOT(sltUpdateStatus(QString, int, int)));
		
		thread->start();

	}

// 
// 	QDir dir;
// 	dir.mkpath(strSaveDir);
// 
// 	for (int i=0; i<nFileNum; i++)
// 	{
// 		QString strCreateDir = strSaveDir;
// 		if (strCreateDir.endsWith('/') == false)
// 		{
// 			strCreateDir += "/";
// 		}
// 		for (int j=0; j<nDirLayerNum; j++)
// 		{
// 			strCreateDir += QString("Dir%1_Layer%2/").arg(i+1).arg(j+1);
// 		}
// 		QDir dir;
// 		dir.mkpath(strCreateDir);
// 
// 		QString strFilePath = strCreateDir + QString("File%1.dat").arg(i+1);
// 		QFile file(strFilePath);
// 		if(file.open(QIODevice::WriteOnly))
// 		{
// 			file.resize(nFileKB * 1024);
// 			file.close();
// 			ui.progressBar->setValue(((float)(i + 1)/(float)nFileKB) * 100.0);
// 			ui.listWidget->addItem(strFilePath);
// 			ui.listWidget->scrollToBottom();
// 		}
// 		QCoreApplication::processEvents();
// 	}
}

